from django.shortcuts import render

# Create your views here.
import uuid
from rest_framework.views import APIView
from rest_framework.request import Request
from rest_framework.response import Response


from app01.permission import *
from app01.auth import TokenAuthentication
from app01 import models



class AuthView(APIView):
    """
    用户登录认证
    """

    permission_classes = []

    def post(self,request,*args,**kwargs):
        print(request.data) # {"username":"yaozhimeng","password":123}
        username = request.data.get('username')
        password = request.data.get('password')

        user_obj = models.UserInfo.objects.filter(username=username,password=password).first()
        if not user_obj:
            return Response({"code":1000,"data":"用户名或密码错误"})
        token = str(uuid.uuid4())

        user_obj.token = token
        user_obj.save()

        return Response({"code":200,"data":{"name":username,"token":token}})




class OrderView(APIView):
    authentication_classes = [TokenAuthentication,]

    def get(self,request,*args,**kwargs):
        print(request.user)
        return Response({"code":200,"data":[11,22,33]})


